package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;

import com.oa.modules.approvalsec1.entity.NewsNoticeEntity;
import com.oa.modules.approvalsec1.service.NewsNoticeService;




/**
 *
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-03-26 10:09:17
 */
@RestController
@RequestMapping("news/notice")
@Api(tags = "通知相关接口")
public class NewsNoticeController extends AbstractController {
    @Autowired
    private NewsNoticeService newsNoticeService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = newsNoticeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id){
		NewsNoticeEntity newsNotice = newsNoticeService.getById(id);
        return R.ok().put("newsNotice", newsNotice);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody NewsNoticeEntity newsNotice){
        newsNotice.setId(AppUtils.getUUID());
        newsNotice.setCreateDate(new Date());
        newsNotice.setCreateOper(getUserId().toString());
		newsNoticeService.save(newsNotice);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody NewsNoticeEntity newsNotice){
        newsNotice.setUpdateDate(new Date());
        newsNotice.setUpdateOper(getUserId().toString());
		newsNoticeService.updateById(newsNotice);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids){
		newsNoticeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 已阅读
     */
    @GetMapping("/viewed/{userId}")
    @ApiOperation(value = "已阅读")
    public R viewed(@PathVariable("userId") String userId){
        List<NewsNoticeEntity> newsNotice = newsNoticeService.list(new QueryWrapper<NewsNoticeEntity>().eq("PUSH_USER_ID", userId));
        String[] ids = new String[newsNotice.size()];
        for (int i=0;i<newsNotice.size();i++){
            ids[i] = newsNotice.get(i).getId();
        }
        newsNoticeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
